/**
 * Created by jiachenpan on 16/11/18.
 */
import { Message, Loading } from 'element-ui'
export function isvalidUsername(str) {
  return true
}

/* 合法uri*/
export function validateURL(textval) {
  const urlregex = /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/
  return urlregex.test(textval)
}

/* 小写字母*/
export function validateLowerCase(str) {
  const reg = /^[a-z]+$/
  return reg.test(str)
}

/* 大写字母*/
export function validateUpperCase(str) {
  const reg = /^[A-Z]+$/
  return reg.test(str)
}

/* 大小写字母*/
export function validatAlphabets(str) {
  const reg = /^[A-Za-z]+$/
  return reg.test(str)
}

/**
 * validate email
 * @param email
 * @returns {boolean}
 */
export function validateEmail(email) {
  const re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
  return re.test(email)
}

export function validateAddress(context) {
  return (rule, value, callback) => {
    const { provinceId, cityId, countyId, communityName, communityId, buildingName, buildingId, detailAddress } = value
    if (!provinceId && !cityId && !countyId) {
      callback(new Error('地址不能为空'))
    } else if (context.showCommunityInput && !communityName) {
      callback(new Error('小区名称不能为空'))
    } else if (context.showCommunitySelector && !communityId) {
      callback(new Error('请选择小区'))
    } else if (context.showBuildingInput && !buildingName) {
      callback(new Error('楼栋名称不能为空'))
    } else if (context.showBuildingSelector && !buildingId) {
      callback(new Error('请选择楼栋'))
    } else if (context.showDetailAddress && !detailAddress) {
      callback(new Error('详细地址不能为空'))
    } else {
      callback()
    }
  }
}

/**
 * 验证是否是小数
 * @param value
 * @returns {boolean}
 */
export function checkNum(value,msg) {
    if (!value) {
      Message({
        message: msg + '不能为空',
        type: 'error',
        duration: 5 * 1000
      })
      return false;
    }
    var regu = "^[0-9]+\\.?[0-9]*$"; // 小数测试
    var re = new RegExp(regu);
    if(re.test(value)){
      return true;
    }else{
      Message({
        message: msg + '请输入数字值',
        type: 'error',
        duration: 5 * 1000
      })
    }
    return false;
}

export function  checkPhone(phoneNum) {
  let reg = /^1(3[0-9]|4[5,7]|5[0,1,2,3,5,6,7,8,9]|6[2,5,6,7]|7[0-9]|8[0-9]|9[1,8,9])\d{8}$/;
  if (reg.test(phoneNum)) {
    return true
  } else {
    Message({
      message:'无效的手机号',
      type: 'error',
      duration: 5 * 1000
    })
    return false
  }
}

/**
 * 检查用户名
 * @param userName
 * @returns {boolean}
 */
export function checkUserName(userName) {
  if(userName){
    var res = /^[a-zA-Z0-9]{1}([a-zA-Z0-9]|_){2,20}$/.test(userName);
    if(!res){
      Message({
        message:'账号只能以字母或数字开头,且只能包含_线.',
        type: 'error',
        duration: 5 * 1000
      })
      return false;
    }else{
      return true;
    }
  }
  return false;
}
